package com.example.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.entity.Comment;
import com.example.demo.mapper.CommentMapper;
import com.example.demo.service.CommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author admin
 * @since 2021-05-18
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {


    @Autowired
    private CommentMapper commentMapper;

    //创建一个临时的集合，用来保存二级评论
    private List<Comment> lingshi = new ArrayList<>();

    //comments集合是父评论的集合
    public void combineChildren(List<Comment> comments)
    {

        //进来的时候，comments集合已经是父级评论的集合了
        for ( Comment comment : comments )
        {
            //首先获取父级评论中的第一层子评论
            QueryWrapper<Comment> commentQueryWrapper = new QueryWrapper<>();

            commentQueryWrapper.eq("superior_id",comment.getId());

            List<Comment> comments1 = commentMapper.selectList(commentQueryWrapper);

            //然后遍历第一层的子评论
            for (Comment comment1 : comments1)
            {
                recursively(comment1);
            }

            comment.setNext(lingshi);

            lingshi = new ArrayList<>();
        }

    }

    private void recursively(Comment comment1) {
        //顶点节点添加到临时存放集合
        if (!lingshi.contains(comment1))
        {

            QueryWrapper<Comment> commentQueryWrapper = new QueryWrapper<>();

            commentQueryWrapper.eq("id",comment1.getSuperiorId());

            Comment comment = commentMapper.selectOne(commentQueryWrapper);

            comment1.setParentname(comment.getUserNickname());

            //上面四行代码是获取该条评论的上级评论，然后设置parent属性（设置父级评论的名称）

            lingshi.add(comment1);

        }


        //如果再查看该节点是否有字级评论
        QueryWrapper<Comment> commentQueryWrapper = new QueryWrapper<>();

        commentQueryWrapper.eq("superior_id",comment1.getId());

        List<Comment> comments = commentMapper.selectList(commentQueryWrapper);

        if (comments.size() > 0)
        {
            for (Comment comment : comments)
            {

                QueryWrapper<Comment> commentQueryWrapper1 = new QueryWrapper<>();

                commentQueryWrapper1.eq("id",comment.getSuperiorId());

                Comment comment2 = commentMapper.selectOne(commentQueryWrapper1);

                comment.setParentname(comment2.getUserNickname());

                //上面四行代码是获取该条评论的上级评论，然后设置parent属性（设置父级评论的名称）

                lingshi.add(comment);

                commentQueryWrapper.clear();

                commentQueryWrapper.eq("superior_id",comment.getId());

                if (commentMapper.selectList(commentQueryWrapper).size() > 0)
                {
                    recursively(comment);
                }
            }
        }


    }

}
